//
//  KKAppUtils.m
//  DoMi
//
//  Created by 康凯 on 2016/11/11.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKAppUtils.h"

@implementation KKAppUtils

#pragma mark 判断字符串是否为空
+ (BOOL) isEmptyString:(NSString *)string {
    if (string == nil || string.length==0 || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    return NO;
}
#pragma mark 时间戳转时间
+ (NSString *)timeStampToDate:(NSString *)time{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy/MM/dd"];
    
    NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:[time doubleValue]];
    return [formatter stringFromDate: detailDate];
}
#pragma mark 时间戳转时间
+ (NSString *)timeStampToDate1:(NSString *)time{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:[time doubleValue]];
    return [formatter stringFromDate: detailDate];
}
#pragma mark 获取当前view的controller
+ (UIViewController *)getCurrentVisiableVC:(UIView *)currentView {
    id target=currentView;
    while (target) {
        target = ((UIResponder *)target).nextResponder;
        if ([target isKindOfClass:[UIViewController class]]) {
            break;
        }
    }
    return target;
}
#pragma mark 计算字体
+(CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize{
    NSDictionary *attrs=@{NSFontAttributeName:font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
#pragma mark toast
+ (void) toast:(NSString *)str {
    MBProgressHUD *tip=[MBProgressHUD showHUDAddedTo:appWindow animated:YES];
    tip.mode=MBProgressHUDModeText;
    tip.label.text=str;
    [tip hideAnimated:YES afterDelay:1];
}
#pragma mark toast
+ (void) toastLongText:(NSString *)str {
    MBProgressHUD *tip=[MBProgressHUD showHUDAddedTo:appWindow animated:YES];
    tip.mode=MBProgressHUDModeText;
    tip.detailsLabel.text=str;
    tip.detailsLabel.font=[UIFont systemFontOfSize:16];
    [tip hideAnimated:YES afterDelay:2];
}
#pragma mark 秒转时分秒
+(NSString *)timeformatFromSeconds:(int)seconds
{
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02d",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02d",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02d",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@时%@分%@秒",str_hour,str_minute,str_second];
    return format_time;
}
+(NSString *)getCurrentTime:(NSString *)formatter{
    //获取当前时间，日期
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    return dateString;
}
+(NSTimeInterval)getCurrentSecond:(NSString *)time formatter:(NSString *)format{
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSDate *date=[formatter dateFromString:time];
    NSTimeInterval startTime=[date timeIntervalSinceNow];
    return startTime;
}
@end
